import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import VueJsx from '@vitejs/plugin-vue-jsx';
// 定义路径别名
import path from 'path';
// 按需引入Elementuiplus
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';

const pathSrc = path.resolve(__dirname, 'src');

// https://vitejs.dev/config/
export default ({ mode }) =>
    defineConfig({
        plugins: [
            vue(),
            VueJsx(),
            AutoImport({
                resolvers: [ElementPlusResolver()],
            }),
            Components({
                resolvers: [ElementPlusResolver()],
            }),
        ],
        server: {
            port: 8080,
            hmr: true,
            proxy: {
                '/api': {
                    target: loadEnv(mode, process.cwd()).VITE_APP_HOST,
                    changeOrigin: true,
                    rewrite: path => path.replace(/^\/api/, ''),
                },
                '/ws': {
                    target: 'https://apis.map.qq.com',
                    changeOrigin: true,
                },
            },
        },
        resolve: {
            alias: {
                '@': pathSrc,
            },
            extensions: ['.js', '.ts', '.tsx', '.jsx', '.vue'],
        },
    });
